import { takeItem } from "../utils/mirUtil";

const 回血药品配置 = [
    {
        name: '疗伤药',
        percent: 15,
        hp: 150,
    },
    {
        name: '万年雪霜',
        percent: 10,
        hp: 100,
    },
    {
        name: '强效太阳水',
        percent: 5,
        hp: 50,
    }
]


export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let 回血HP = Player.V.回血HP || 0
    let 回血MP = Player.V.回血MP || 0
    let itemString = `<{S=HP低于${回血HP}%开始回血;C=255;X=0;Y=0}><{S=设置;C=251;Y=0;/@@人物回血.InputString1(请输入设置血量)}>`;
    itemString += `<{S=MP低于${回血MP}%开始回血;C=255;X=0;Y=15}><{S=设置;C=251;Y=15;/@@人物回血.InputString2(请输入设置血量)}>`;
    let y = 30;
    for (let i = 0; i < 回血药品配置.length; i++) {
        let item = 回血药品配置[i].name
        let check = Player.V[`hpcheck${i}`]
        itemString += `<{S=使用${item}回血(${check ? '已开启' : '已关闭'});C=255;X=0;Y=${y};}>`;
        itemString += `<{S=${check ? '关闭' : '开启'};C=251;Y=${y};/@人物回血.设置回血开关(${i})}>`
        y += 15;
    }
    Npc.Say(Player, itemString)
}


export function InputString1(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.V.回血HP = Args.Int[0];
    Main(Npc, Player, Args)
}
export function InputString2(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.V.回血MP = Args.Int[0];
    Main(Npc, Player, Args)
}
export function 设置回血开关(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let index = Args.Int[0];
    let key = `hpcheck${index}`
    Player.V[key] = !Player.V[key]
    Main(Npc, Player, Args)
}

export function 人物回血执行(Player: TPlayObject) {
    if (Player.IsAdmin) {
        Player.HP += 10000;
        Player.MP += 10000;
        return
    }

    let 回血HP = Player.V.回血HP || 0
    let 回血MP = Player.V.回血MP || 0
    let percentHp = Player.HP / Player.MaxHP * 100
    let percentMp = Player.MP / Player.MaxMP * 100
    if (percentHp >= 回血HP && percentMp >= 回血MP) {
        return
    }

    for (let i = 0; i < 回血药品配置.length; i++) {
        let item = 回血药品配置[i]
        let check = Player.V[`hpcheck${i}`]
        if (check) {
            if (takeItem(Player, item.name, 1, "回血", false)) {
                let addHp = Math.floor(Player.MaxHP * item.percent / 100) + item.hp
                let addMp = Math.floor(Player.MaxMP * item.percent / 100) + item.hp
                Player.HP += addHp;
                Player.MP += addMp;
                return
            }
        }
    }
}